<?php

namespace wangk\openapi\openapi\oAuth;


use wangk\openapi\openapi\config\Config;

class OAuthClient
{

    private $request_id;//自己的app_key
    private $request_secret;//自己的app_secret
    private $config;

    public function __construct(Config $config)
    {
        $this->config = $config;
        $this->request_id = $config->getAppKey();
        $this->request_secret = $config->getAppSecret();
    }

    /**得到token和refresh_token*/
    public function getAccessToken($scope = 'all', $tUid = 0)
    {
        $param = [
            'timestamp' => time(),
            'request_id' => $this->request_id,
            'secret_key' => $this->request_secret,
            'scope' => $scope,
            't_uid' => $tUid,
            'response_type' => 'token'
        ];
        $tokenUrl = $this->config->requestUrl() . 'open/auth/authorize';
        return $this->request($tokenUrl,$param);
    }

    /**租户得到app的token和refresh_token*/
    public function getAppAccessToken($scope = 'all', $tUid = 0,$clientId = '',$state='te')
    {
        $param = [
            'timestamp' => time(),
            'request_id' => $this->request_id,
            'secret_key' => $this->request_secret,
            'scope' => $scope,
            't_uid' => $tUid,
            'state'=>$state,
            'client_id'=>$clientId,
            'response_type' => 'token'
        ];
        $tokenUrl = $this->config->requestUrl() . 'open/auth/authorize_t';
        return $this->request($tokenUrl,$param);
    }

    private function request($action,$param)
    {
        $param['sign'] = RequestClient::signature($param);
        unset($param['secret_key']);
        return RequestClient::request($action, $param);
    }





}